package com.lzkj.baotouhousingfund.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.coderc.library.RxDeviceTool;
import com.coderc.library.RxTool;
import com.lzkj.baotouhousingfund.BuildConfig;
import com.lzkj.baotouhousingfund.R;
import com.lzkj.baotouhousingfund.di.component.AppComponent;
import com.lzkj.baotouhousingfund.di.component.DaggerAppComponent;
import com.lzkj.baotouhousingfund.di.module.AppModule;
import com.lzkj.baotouhousingfund.di.module.HttpModule;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.HashSet;
import java.util.Set;


/**
 * @author Chengs
 * @ClassName: App
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2017/10/16 11:21
 */

public class App extends Application {

    private static App           instance;
    public static  AppComponent  appComponent;
    private        Set<Activity> allActivities;

    public static int   SCREEN_WIDTH  = -1;
    public static int   SCREEN_HEIGHT = -1;
    public static float DIMEN_RATE    = -1.0F;
    public static int   DIMEN_DPI     = -1;


    public static synchronized App getInstance() {
        return instance;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        RxTool.init(this);
        //初始化屏幕宽高
        getScreenSize();
        initBugly();
        initSmartRefreshLayout();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : allActivities) {
            if (activity.getClass().equals(cls)) {
                activity.finish();
            }
        }
    }

    public void exitApp(boolean isKill) {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    act.finish();
                }
            }
        }
        if (isKill) {
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        if(!getAppComponent().realmHelper().isCloseRealm()){
            getAppComponent().realmHelper().closeRealm();
        }
    }

    public void getScreenSize() {
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        Display display = windowManager.getDefaultDisplay();
        display.getMetrics(dm);
        DIMEN_RATE = dm.density / 1.0F;
        DIMEN_DPI = dm.densityDpi;
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        if (SCREEN_WIDTH > SCREEN_HEIGHT) {
            int t = SCREEN_HEIGHT;
            SCREEN_HEIGHT = SCREEN_WIDTH;
            SCREEN_WIDTH = t;
        }
    }

    public static AppComponent getAppComponent() {
        if (appComponent == null) {
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(instance))
                    .httpModule(new HttpModule())
                    .build();
        }
        return appComponent;
    }

    private void initBugly() {
        Context context = getApplicationContext();
        String packageName = context.getPackageName();
        String processName = RxDeviceTool.getProcessName(android.os.Process.myPid());
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        CrashReport.initCrashReport(context, Constants.BUGLY_ID, BuildConfig.DEBUG, strategy);
    }

    private void initSmartRefreshLayout() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                BezierRadarHeader header = new BezierRadarHeader(context);
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
                return header;
            }
        });
    }

}
